//
//  JourneyDetilViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-24.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyDetilViewController.h"
#import "JourneryNetHandler.h"
#import "JourneyDetilDataSource_Detil.h"
#import "JourneryNetHandler.h"
#import "UIImageView+WebCache.h"

static CGFloat ImageHeight  = 150.0;
static CGFloat ImageWidth  = 320.0;

@interface JourneyDetilViewController ()<UIScrollViewDelegate>

@property (nonatomic , retain)UIImageView *imgProfile;
@property (nonatomic , retain)NSDictionary *resultDic;
@property (nonatomic , retain)NSMutableArray *resultArr;
@property (nonatomic , retain)UILabel *nameLable;
@property (nonatomic , retain)UILabel *recommended_resonLable;
@end

@implementation JourneyDetilViewController
#pragma mark data
- (void)handlerNet

{
    NSLog(@"3333333");
    NSString *str = [[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.journeyData.type,self.journeyData.idNumber] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"!!!!!!!%@",str);
    [JourneryNetHandler getDataWithUrl:str completion:^(NSData *data) {
        self.resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil ];
        
        self.resultArr = [_resultDic objectForKey:@"hottest_places"];
        NSString *str = [[_resultArr objectAtIndex:0] valueForKey:@"photo"];
        [self.imgProfile setImageWithURL:[NSURL URLWithString: [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        self.nameLable.text = [_resultDic objectForKey:@"name"];
        self.recommended_resonLable.text = [_resultDic objectForKey:@"recommended_reason"];
    }];
  
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset   = self.scrollView.contentOffset.y;
    CGFloat xOffset   = self.wrapperScroll.contentOffset.x;
    
    if (yOffset < 0) {
        
        CGFloat pageWidth = self.wrapperScroll.frame.size.width;
        int page = floor((self.wrapperScroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

        CGFloat factor = ((ABS(yOffset)+ ImageHeight )*ImageWidth)/ ImageHeight;
        
        CGRect f = CGRectMake((-(factor-ImageWidth)/2) + 320 *page, 20, ImageWidth, ImageHeight);
        f.size.height = ImageHeight+ABS(yOffset);
        f.size.width = factor;
        _imgProfile.frame = f;
        
        CGRect frame = self.imgContainer.frame;
        frame.origin.y = 0;
        self.imgContainer.frame = frame;
    } else {
        self.imgContainer.contentOffset = CGPointMake(xOffset, yOffset);
    }
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handlerNet];
    [self addView];
}

#pragma mark 加载视图
- (void)addView
{
    self.imgContainer  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ImageWidth, self.view.bounds.size.height)];
    self.imgContainer.backgroundColor = [UIColor clearColor];
    self.imgContainer.contentSize = CGSizeMake(ImageWidth, self.view.bounds.size.height);
    
    [self.view addSubview:self.imgContainer];
    self.imgProfile = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _imgContainer.frame.size.width, ImageHeight)];
    [self.imgContainer addSubview:self.imgProfile];
    
    UIView *detilView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height)];
    detilView.backgroundColor = [UIColor whiteColor];
    detilView.layer.cornerRadius = 10;
    detilView.clipsToBounds = YES;
    CGRect frame = detilView.frame;
    frame.origin.y = ImageHeight;
    detilView.frame = frame;
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.contentSize = CGSizeMake(320, detilView.frame.size.height+ImageHeight);
    //    [self.scrollView addSubview:self.wrapperScroll];
    [self.scrollView addSubview:detilView];
    [self.view addSubview:self.scrollView];
    
    self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, detilView.frame.size.width, 50)];
//    _nameLable.backgroundColor = [UIColor redColor];
    _nameLable.textAlignment = NSTextAlignmentCenter;
    [detilView addSubview:_nameLable];
    
    self.recommended_resonLable = [[UILabel alloc]initWithFrame:CGRectMake(0,90, detilView.frame.size.width, 30)];
    _recommended_resonLable.textAlignment = NSTextAlignmentCenter;
    _recommended_resonLable.font = [UIFont fontWithName:@"ChalkboardSE-Light" size:10];
//    _recommended_resonLable.backgroundColor = [UIColor redColor];
    [detilView addSubview:_recommended_resonLable];
    
    CGRect btuFram = CGRectMake(detilView.frame.size.width / 3, _recommended_resonLable.frame.origin.y + 20, detilView.frame.size.width / 3, 50);
    UIButton *ratingBtu = [UIButton buttonWithTitle:@"添加点评" frame:btuFram target:self action:@selector(ratingBtuAction:)];
    [detilView addSubview:ratingBtu];
    
    
}

#pragma  mark MoreBtu
- (void)ratingBtuAction : (UIButton *)btu
{
    NSLog(@"111");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    CGRect bounds = self.view.bounds;
    self.scrollView.frame = bounds;
    self.navigationController.navigationBar.translucent = YES;
//    [[UINavigationBar appearance] setTintColor:[UIColor colorWithWhite:0.0 alpha:0.3]];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
